package org.bud.data.bit.fmt;

import java.math.BigDecimal;
import java.util.function.Function;
import java.util.function.UnaryOperator;

/**
 * @author shaun
 * @date 2024/7/21
 * @instruction excel最后执行的一种数据格式处理
 */
public abstract class DefaultNumberFmt extends BaseFmtData<BigDecimal> {
    @Override
    public BigDecimal origin(Object obj) {
        if(obj == null){
            return null;
        }
        if(obj instanceof Double){
            if(((Double) obj).isInfinite() || ((Double) obj).isNaN()){
                return null;
            }
            return BigDecimal.valueOf((Double) obj);
        }
        if(obj instanceof Float){
            if(((Float) obj).isInfinite() || ((Float) obj).isNaN()){
                return null;
            }
            return BigDecimal.valueOf((Float) obj);
        }
        return new BigDecimal(obj.toString());
    }

    @Override
    public Function<BigDecimal, ?> fain() {
        return b -> b;
    }

    @Override
    protected UnaryOperator<BigDecimal> fmt() {
        return r -> r;
    }
}
